const app = getApp();
Page({

  data: {
    ctxWidth: 360,
    ctxHeight: '',
  },

  onLoad: function (options) {
    let that = this;
    let goodsDetail = wx.getStorageSync('goodsDetail');
    // wx.removeStorageSync('goodsDetail');
    that.setData({
      options,
      goodsDetail
    })


    wx.showLoading({
      title: '正在努力生成中...',
    })
    let poster = goodsDetail.image.file_path;
    let qrcode = goodsDetail.qrcode;
    let pixelRatio = wx.getSystemInfoSync().pixelRatio;
    let ctxWidth = that.data.ctxWidth;
    let strLen = that.strlen(38);
    let title = goodsDetail.goods_name.length > strLen ? goodsDetail.goods_name.slice(0, strLen) + '...' : goodsDetail.goods_name;
    // let title = goodsDetail.goods_name.slice(0, strLen);
    let goods_price = goodsDetail.goods_price;
    let line_price = goodsDetail.line_price;
    // 获取banner
    wx.getImageInfo({
      src: poster,
      complete: function (res) {
        let bannerHeight = res.height * ctxWidth / res.width;
        that.setData({
          pixelRatio,
          banner_get: res.path,
          bannerHeight: bannerHeight,
          ctxHeight: bannerHeight + 180
        })
        // 获取二维码
        wx.getImageInfo({
          src: qrcode,
          complete: function (res) {
            that.setData({
              code_get: res.path
            })

            let ctxHeight = that.data.ctxHeight;
            const ctx = wx.createCanvasContext('main')
            const bannerHeight = that.data.bannerHeight
            const banner_get = that.data.banner_get;
            const code_get = that.data.code_get;

            // 画背景色
            ctx.save()
            ctx.beginPath()
            ctx.setFillStyle('#ffffff')
            ctx.fillRect(0, 0, ctxWidth, ctxHeight)
            ctx.restore()

            // banner
            ctx.beginPath()
            ctx.drawImage(banner_get, 0, 0, ctxWidth, bannerHeight);
            ctx.restore()

            ctx.beginPath()
            ctx.setTextBaseline('top')
            ctx.setFillStyle('#000000')
            ctx.setFontSize(16)
            ctx.setTextAlign('left')
            ctx.fillText(title, 20, bannerHeight + 15)

            ctx.setFillStyle('#FD0100')
            ctx.setFontSize(18)
            ctx.setTextAlign('left')

            ctx.fillText("￥"+goods_price, 20, bannerHeight + 50)
            let goods_price_width = ctx.measureText("￥" + goods_price).width;
            ctx.setFillStyle('#a8a8a8')
            ctx.setFontSize(14)
            ctx.fillText("￥" + line_price, goods_price_width + 30, bannerHeight + 52)
          
            let line_price_width = ctx.measureText("￥" + line_price).width;
            ctx.beginPath()
            ctx.setStrokeStyle('#a8a8a8')
            ctx.moveTo(goods_price_width + 30 , bannerHeight + 60)
            ctx.lineTo(goods_price_width + line_price_width + 35, bannerHeight + 60)
            ctx.stroke()

            ctx.beginPath()
            ctx.setStrokeStyle('#eeeeee')
            ctx.moveTo(20, bannerHeight + 80)
            ctx.lineTo(ctxWidth - 20, bannerHeight + 80)
            ctx.stroke()
            ctx.restore()

            ctx.beginPath();
            ctx.drawImage(code_get, 20, ctxHeight - 90, 80, 80);

            ctx.beginPath()
            ctx.setFillStyle('#000000')
            ctx.setFontSize(16)
            ctx.setTextAlign('left')
            ctx.fillText("长按识别二维码查看精彩!", 120, ctxHeight - 60)
            ctx.setFillStyle('#969696')
            ctx.setFontSize(12)
            ctx.fillText("政企物流宝办公商城", 120, ctxHeight - 30)

            wx.hideLoading()
            ctx.draw(false, function (e) {
              wx.canvasToTempFilePath({
                x: 0,
                y: 0.,
                width: that.data.ctxWidth,
                height: that.data.ctxHeight,
                destWidth: that.data.ctxWidth * that.data.pixelRatio,
                destHeight: that.data.ctxHeight * that.data.pixelRatio,
                canvasId: 'main',
                success(res){
                  that.setData({
                    canvasImg: res.tempFilePath
                  })
                }
              })
            })

          }
        })
      }
    })

  },

  onShow: function () {

  },
  strlen(strLen){  
    var len = 0;  
    let title = this.data.goodsDetail.goods_name;
    for (var i=0; i<strLen;) {   
      var c = title.charCodeAt(i);   
      if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f)) {   
        i++;     
      } else {   
        i+=2;   
      }   
      len++;   
    }   
    return len;  
  },
  downloadImg: function () {
    let that = this;
    wx.canvasToTempFilePath({
      x: 0,
      y: 0.,
      width: that.data.ctxWidth,
      height: that.data.ctxHeight,
      destWidth: that.data.ctxWidth * that.data.pixelRatio,
      destHeight: that.data.ctxHeight * that.data.pixelRatio,
      canvasId: 'main',
      success: function (res) {
        console.log(res.tempFilePath)
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function (res) {
            wx.showToast({
              title: '保存成功',
              icon: 'success',
            })
          },
          fail: function (res) {
            if (res.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || res.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
              // 这边微信做过调整，必须要在按钮中触发，因此需要在弹框回调中进行调用
              wx.showModal({
                title: '提示',
                content: '需要您授权保存相册',
                showCancel: false,
                success: modalSuccess => {
                  wx.openSetting({
                    success(settingdata) {
                      console.log("settingdata", settingdata)
                      if (settingdata.authSetting['scope.writePhotosAlbum']) {
                        wx.showModal({
                          title: '提示',
                          content: '获取权限成功,再次点击图片即可保存',
                          showCancel: false,
                        })
                      } else {
                        wx.showModal({
                          title: '提示',
                          content: '获取权限失败，将无法保存到相册哦~',
                          showCancel: false,
                        })
                      }
                    },
                    fail(failData) {
                      console.log("failData", failData)
                    },
                    complete(finishData) {
                      console.log("finishData", finishData)
                    }
                  })
                }
              })
            }
          }
        })
      }
    })
  },
  onShareAppMessage: function () {
    let that = this;
    return{
      path: '/pages/goodsDetail/goodsDetail?id=' + that.data.options.id + '&specType=' + that.data.options.specType
    }
  }
})